<?php
/** 
 * $Source: /cvsroot/pn-commerce/pn-commerce/pntemplates/plugins/function.pncUnfoldTree.php,v $
 * $Author: landseer $
 * $Revision: 1.6 $
 * $Date: 2004/05/05 12:45:48 $
 *
 * Type: Function
 *
 * assigns to the variable "unfolded_"+name the 1 dimensional array with additional depth information
 * @return array 
 */
function smarty_function_pncUnfoldTree_rec_helper($depth_offset,$array, $open, &$result)//This function is not for direct calls, just for recursion!
{
    foreach($array as $line)
    {
        $newline=array();
        $children=array();
        //First finish the actual array(dataset)
	    foreach($line as $key=>$detail)
        {
            if(is_array($detail))$children[]=$detail; 
            else $newline[$key]=$detail;
        }
        $newline['depth']=$depth_offset;
        $newline['hasChildren']=count($children[0])>0;
        $newline['open'] = (array_key_exists( $newline['CategoryID'], $open ) == true ) ? true : false;
        $result[]=$newline;
        //now add children behind
        foreach($children as $key=>$detail)
        {
            smarty_function_pncUnfoldTree_rec_helper($depth_offset+1,$detail, $open, $result);
        }
    }
    return;
}

function smarty_function_pncUnfoldTree($params, &$smarty)
{ 
        $result=array();
        if($params['name']=='')$params['name']='category';
        $open = pnSessionGetVar("Open");
        // if $open is a serialized array we unserialize and use it, if not we
        // use an empty array instead. This avoids a warning.
        $open = (@unserialize($open)=="") ? array() : unserialize($open); 
        smarty_function_pncUnfoldTree_rec_helper(0, $params['data'], $open, $result );
        $smarty->assign( 'unfolded_'.$params['name'], $result );
//      echo 'unfolded_'.$params['name']."<pre>";print_r($result);echo "</pre>";
}
?>
